import 'package:flutter/material.dart';

///zhudaihao
///2024-1-30
///切换按钮
class MySwitch extends StatefulWidget {
  ///标题
  final String? title;

  ///按钮左边文本
  final String? leftText;

  ///按钮右边文本
  final String? rightText;

  ///默认选择哪个按钮 true 左边
  final bool isDefaultCheck;

  ///点击确定回调
  final Function(String v)? onCLick;

  const MySwitch(
      {this.title,
      @required this.leftText,
      @required this.rightText,
      this.isDefaultCheck = true,
      this.onCLick,
      Key? key})
      : super(key: key);

  @override
  State<MySwitch> createState() => _MySwitchState();
}

class _MySwitchState extends State<MySwitch> {
  bool isCheck = true;
  String checkText = "";

  @override
  void initState() {
    if (widget.isDefaultCheck) {
      checkText = widget.leftText ?? '';
    } else {
      checkText = widget.rightText ?? '';
    }
    isCheck = widget.isDefaultCheck;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisSize: MainAxisSize.min,
      children: [
        Padding(
          padding: const EdgeInsets.only(top: 32, bottom: 24),
          child: Text(
            widget.title ?? '',
            style: const TextStyle(
              color: Colors.white,
              fontSize: 20,
              fontWeight: FontWeight.w400,
            ),
          ),
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            InkWell(
              child: Container(
                alignment: Alignment.center,
                width: 132,
                height: 42,
                decoration: BoxDecoration(
                    borderRadius: const BorderRadius.only(
                        topLeft: Radius.circular(10),
                        bottomLeft: Radius.circular(10)),
                    color: isCheck
                        ? const Color(0xffDFE0E7)
                        : const Color(0xff1C1C20)),
                child: Text(
                  '${widget.leftText}',
                  style: TextStyle(
                      color: isCheck ? const Color(0xff20212A) : Colors.white),
                ),
              ),
              onTap: () {
                setState(() {
                  isCheck = !isCheck;
                });
              },
            ),
            InkWell(
              child: Container(
                alignment: Alignment.center,
                width: 132,
                height: 42,
                decoration: BoxDecoration(
                    borderRadius: const BorderRadius.only(
                        topRight: Radius.circular(10),
                        bottomRight: Radius.circular(10)),
                    color: !isCheck
                        ? const Color(0xffDFE0E7)
                        : const Color(0xff1C1C20)),
                child: Text(
                  '${widget.rightText}',
                  style: TextStyle(
                      color: !isCheck ? const Color(0xff20212A) : Colors.white),
                ),
              ),
              onTap: () {
                setState(() {
                  isCheck = !isCheck;
                });
              },
            ),
          ],
        ),
        Padding(
          padding:
              const EdgeInsets.only(left: 24, right: 24, top: 24, bottom: 36),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Expanded(
                child: InkWell(
                  onTap: () {
                    Navigator.pop(context);
                  },
                  child: Container(
                    width: 148,
                    height: 44,
                    decoration: BoxDecoration(
                      color: const Color(0xFF262835),
                      borderRadius: BorderRadius.circular(10),
                    ),
                    alignment: Alignment.center,
                    child: const Text(
                      "取消",
                      style: TextStyle(
                        color: Color(0xFFDFE0E7),
                        fontSize: 14,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ),
                ),
              ),
              const SizedBox(
                width: 16,
              ),
              Expanded(
                child: InkWell(
                  onTap: () {
                    if (widget.onCLick != null) {
                      widget.onCLick!(checkText);
                    }
                  },
                  child: Container(
                    width: 148,
                    height: 44,
                    decoration: BoxDecoration(
                      color: const Color(0xFF1157FF),
                      borderRadius: BorderRadius.circular(10),
                    ),
                    alignment: Alignment.center,
                    child: const Text(
                      '保存',
                      style: TextStyle(
                        color: Color(0xFFDFE0E7),
                        fontSize: 14,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}
